package com.dc.wifi.main.utils;

import android.content.Context;
import android.media.MediaPlayer;

import com.dc.wifi.R;

public class AudioPlayer222 {
    private MediaPlayer mediaPlayer;
    private float playCountPerSecond;

    public AudioPlayer222(Context context, float playCountPerSecond) {
        this.playCountPerSecond = playCountPerSecond;
        mediaPlayer = MediaPlayer.create(context, R.raw.zzzz);
        mediaPlayer.setLooping(true); // 设置循环播放
    }

    // 播放音频方法
    public void play() {
        if (mediaPlayer != null && !mediaPlayer.isPlaying()) {
            mediaPlayer.start();
            setPlayCountPerSecond(playCountPerSecond); // 设置初始播放速率
        }
    }

    // 停止音频方法
    public void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            mediaPlayer.seekTo(0); // 重置到音频开始位置
        }
    }

    // 设置每秒播放次数
    public void setPlayCountPerSecond(float playCountPerSecond) {
        this.playCountPerSecond = playCountPerSecond;
        if (mediaPlayer != null) {

            mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(playCountPerSecond));
        }
    }

    // 释放资源方法（在不再需要播放器时调用）
    public void release() {
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }
}